package educar.materia;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import educar.materia.ModifMateriaVentana;
import educar.materia.MateriaController;

public class ModifMateriaMediador implements ActionListener{
	
	private ModifMateriaVentana view = new ModifMateriaVentana();
	private String materia;
	
	public ModifMateriaMediador(){
		view.setVisible(true);
		view.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj instanceof JButton)
			try {
				ListenerEvent(((JButton) obj).getName());
			} catch (MateriaNoEncontrada e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		
	}
	
	private void ListenerEvent(String event) throws MateriaNoEncontrada{
		
		if (event.compareTo("Aceptar") == 0){
			String codigo = view.getCodigo();
			if (MateriaController.existe(codigo)){
				Materia mat = MateriaController.getMateria(codigo);
				ModificarMateriaMediador mod = new ModificarMateriaMediador(mat);
				view.dispose();
			}
		}
		else if (event.compareTo("Cancelar") == 0){
			view.dispose();
		}
	}
}
